package com.example.ktproject

import android.inputmethodservice.Keyboard
import android.inputmethodservice.KeyboardView
import android.text.InputType
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

class KeyboardUtil(private var activity: AppCompatActivity) {

    private var mKeyboardView: KeyboardView
    private val provinceKeyboard: Keyboard
    private var abcKeyboard: Keyboard
    private var editText: EditText? = null

    init {
        // 省份键盘
        provinceKeyboard = Keyboard(activity, R.xml.province)
        // abc键盘
        abcKeyboard = Keyboard(activity, R.xml.abc)

        mKeyboardView = activity.findViewById(R.id.keyboard_view)
        mKeyboardView.apply {
            keyboard = provinceKeyboard
            isEnabled = true
            // 设置按键没有点击放大镜显示的效果
            isPreviewEnabled = false
            setOnKeyboardActionListener(object : KeyboardView.OnKeyboardActionListener {
                override fun onPress(primaryCode: Int) {
                }

                override fun onRelease(primaryCode: Int) {
                }

                override fun onText(text: CharSequence?) {
                }

                override fun swipeLeft() {
                }

                override fun swipeRight() {
                }

                override fun swipeDown() {
                }

                override fun swipeUp() {
                }

                override fun onKey(primaryCode: Int, keyCodes: IntArray?) {
                    val editable = editText?.text
                    val start: Int = editText?.selectionStart ?: 0
                    when (primaryCode) {
                        -1 -> changeKeyboard(true)
                        -2 -> changeKeyboard(false)
                        -3 -> {
                            if (start != null) {
                                editable?.delete(0, start)
                            }
                        }
                        else -> {
                            // 清空之前数据
                            editText?.text?.clear()
                            editable?.insert(0, primaryCode.toChar().toString())
                        }
                    }
                }

            })
        }
    }

    fun setEditText(editText: EditText) {
        this.editText = editText
    }

    /**
     * 指定切换软键盘
     * isNumber false 省份软键盘， true 数字字母软键盘
     */
    fun changeKeyboard(isNumber: Boolean) {
        if (isNumber) {
            mKeyboardView.keyboard = abcKeyboard
        } else {
            mKeyboardView.keyboard = provinceKeyboard
        }
    }

    /**
     * 软键盘展示状态
     */
    fun isShow() = mKeyboardView.visibility == View.VISIBLE

    /**
     * 显示软键盘
     */
    fun showKeyboard() {
        val visibility = mKeyboardView.visibility
        if (visibility == View.GONE || visibility == View.INVISIBLE) {
            mKeyboardView.visibility = View.VISIBLE
        }
    }

    /**
     * 隐藏软键盘
     */
    fun hideKeyboard() {
        val visibility = mKeyboardView.visibility
        if (visibility == View.VISIBLE) {
            mKeyboardView.visibility = View.INVISIBLE
        }
    }

    /**
     * 禁掉系统软键盘
     */
    fun hideSoftInputMethod(editText: EditText) {
        activity.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
        editText.inputType = InputType.TYPE_NULL
    }
}